<?php
/**
 * 商品列表控制器
 * 
 * 淘宝图片尺寸(从小到大)
 * 	_40x40.jpg
 * 	_sum.jpg
 * 	_100x100.jpg
 * 	_160x160.jpg
 * 	_b.jpg
 * 	_310x310.jpg
 * 
 * 淘宝商店图标的前缀为：http://logo.taobaocdn.com/shop-logo
 * 
 * @version $Id: Items.php 272 2012-04-21 04:35:40Z linsir123 $
 */
class controllerItems extends AppController 
{
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		parent::__construct();
		
		///
		AppView::addCss('javascript/colorbox/example5/colorbox.css');
		AppView::addJs('colorbox/colorbox/jquery.colorbox.js');
	}
	
	/**
	 * 淘宝推广页(根据分类ID)
	 */
	public function actionList()
	{
		$mParams = M::params();
		
		///
		if ($mParams->getPinyin() == 'zhe') 
			$this->_itemsZhe($mParams);
		else 
			$this->_itemsTaobaoke($mParams);
	}
	
	/**
	 * 商铺
	 */
	public function actionShop()
	{
		$mParams = M::params();
			
		///
		$cId = $mParams->getCid();
		$nick = $mParams->getNick();
		$shop = M::shop()->getOne($nick);
		if (empty($shop)) 
			$this->miss();
		else {
			if (empty($shop['pic_path'])) 
				$shop['pic_path'] = '';
		}
			
		///
		$mNav = M::nav();
		$category = $mNav->getOne($cId);
		
		$tps = $mParams->taobaoke;
		$data = M::item()->taobaokeByShop($tps);
		list($list, $totalPage, $total, $categories) = $data;
		
		///
		$mParams->assign($shop['title'], $totalPage, $category);
		View::display('shop');
		View::assignSet(array(
			'cid' => $cId, 'list' => $list, 'total' => $total, 
			'location' => $mNav->getLocation($cId), 
			
			///
			'shop' => $shop, 
			'categories' => $categories, 
		));
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 列表
	 */
	private function _itemsTaobaoke(& $mParams)
	{
		$mNav = M::nav();
		$cId = $mParams->getCid();
		$keyword = $mParams->getKeyword();
		if (empty($cId) && empty($keyword)) 
			$this->miss();
		elseif ( ! empty($cId)) {
			$category = $mNav->getOne($cId);
			if (empty($category)) 
				$this->miss();
			else {
				if ($_GET['pinyin'] != $category['pinyin']) {
					$_GET['pinyin'] = $category['pinyin'];
					$url = AppView::urlClass($_GET);
					lpUrl::redirect($url);
				}
			}
		}
		
		///
		$tps = $mParams->taobaoke;
		$data = M::item()->taobaoke($tps, true);
		list($list, $totalPage, $total, $users) = $data;
		
		///
		$mParams->assign(null, $totalPage, $category);
		View::display('list');
		View::assignSet(array(
			'cid' => $cId, 'list' => $list, 'total' => $total, 
			'location' => $mNav->getLocation($cId), 
			
			///
			'category' => $category,
			'users' => $users,
		));
	}
	
	/**
	 * 列表
	 */
	private function _itemsZhe(& $mParams)
	{
		$p = $mParams->getP();
		$cId = $mParams->getCid();
		$sort = $mParams->getSort();
		$keyword = $mParams->getKeyword("新款");
		$list = M::rate()->getList($cId, $keyword, $sort, $p*42);
		
		///
		$mParams->assign("折扣啦", count($list)>0?100:0);
		AppView::addJs('jquery.lazyload.js');
		View::display('zhe');
		View::assignSet(array(
			'dh' => App::O('_zheClass'),
			'list' => $list,
		));
	}
}
